<?php

class TournamenttypesPage extends BaseAdminPage {

	/**
	 * Az összes rekord
	 *
	 * @var array
	 */
	private $allRecords;

	function TournamenttypesPage() {
		parent::BaseAdminPage('TournamenttypesPage');
	}

	function enterPage() {
		parent::enterPage();

		// definiáljuk a form elemeit
		FormUtil::addForm("newttype");
		FormUtil::addFormElement("newttype", new FormElementInfo("newname", "text"));
		FormUtil::resetFormValues("newttype");

		// definiáljuk a form elemeit
		FormUtil::addForm("ttype");
		FormUtil::addFormElement("ttype", new FormElementInfo("id", "text"));
		FormUtil::addFormElement("ttype", new FormElementInfo("name", "text"));
		FormUtil::addFormElement("ttype", new FormElementInfo("comment", "text"));
		FormUtil::resetFormValues("ttype");
	}

	function leavePage() {
		parent::leavePage();
			
		FormUtil::removeAllForms();
	}


	public function serveRequest() {
		parent::serveRequest();

		$tpl = BaseAdminPage::loadTemplateFile("tournamenttype.tpl.html");
		TextHandler::insertTextsIntoTemplate($tpl);

		$tpl->setVariable('app_root_url', APP_ROOT_URL);

		$this->allRecords = AdminDBHelper::getTournamentTypes(AdminBase::getCurrentEditlang());
		if(is_null($this->allRecords)) {
			// suxxx!
			handleError(TextHandler::getText('err_readrecordsfailed'));
			$tpl->replaceBlock('tournamenttype', ' ', false);
			$tpl->touchBlock('tournamenttype');
		} else {
			// megvannak a rekordok!

			$action = FunctionLibrary::getGetValue('action');
			if($action) {
				$action = 'action_'.$action;
				$this->$action();
			}

			// kell a hozzáadás művelet
			FormUtil::insertFormValuesToTemplate("newttype", $tpl);
			$tpl->setVariable('addFormAction', LinkProvider::getLink('addTournamentType'));

			if(count($this->allRecords) == 0) {
				$tpl->replaceBlock('tournamenttype', ' ', false);
				$tpl->touchBlock('tournamenttype');
			} else {
				foreach($this->allRecords as $record) {
					$tpl->setCurrentBlock("tournamenttype");

					if(FormUtil::hasFormInstance("ttype", $record['id'])) {
						// szerkesztés alatt áll
						$contentTplHtml = "tournamenttype_edit.tpl.html";
					} else {
						// nem áll szerkesztés alatt
						$contentTplHtml = "tournamenttype_raw.tpl.html";
					}
					$contentTpl = BaseAdminPage::loadTemplateFile($contentTplHtml);
					TextHandler::insertTextsIntoTemplate($contentTpl);

					$contentTpl->setVariable('app_root_url', APP_ROOT_URL);

					$trClasses = array();

					$contentTpl->setVariable("link_del", LinkProvider::getLink("delTournamentType", array("id" => $record['id'])));
					$contentTpl->setVariable("tr_class", implode($trClasses, " "));

					$contentTpl->setVariable("id", $record['id']);

					if(FormUtil::hasFormInstance("ttype", $record['id'])) {
						// szerkesztés alatt áll
						FormUtil::insertFormValuesToTemplate('ttype', $contentTpl, $record['id']);

						$contentTpl->setVariable("formAction", LinkProvider::getLink("modTournamentType", array("id" => $record['id'])));
						$contentTpl->setVariable("link_cancel", LinkProvider::getLink("cancelEditTournamentType", array("id" => $record['id'])));
					} else {
						// nem áll szerkesztés alatt
						if ($record['langcode'] == AdminBase::getCurrentEditlang()) {
							$contentTpl->setVariable("name", $record['name']);
							$contentTpl->setVariable("remark", $record['comment']);
						} else {
							$contentTpl->setVariable("name", '['.$record['name'].'] *');
							$contentTpl->setVariable("remark", '');
						}
						$contentTpl->setVariable("link_edit", LinkProvider::getLink("editTournamentType", array("id" => $record['id'])));
					}
					$tpl->setVariable('tournamementType', $contentTpl->get());
					$contentTpl = null;

					$tpl->parseCurrentBlock();
				}
			}
		}

		return $tpl->get();
	}

	/**
	* Szerkeszt.
	*/
	private function action_editTournamentType() {
		$id = FunctionLibrary::getGetValue('id');
		// $record = array('id' => $id, 'langcode' => AdminBase::getCurrentEditlang());
		$record = AdminDBHelper::getTournamentType($id, AdminBase::getCurrentEditlang());
		AdminPageHelper::action_edit('ttype', $record, "link_tournamenttype");
	}

	/**
	* Szerkesztés visszavonása.
	*/
	private function action_cancelTournamentType() {
		AdminPageHelper::action_cancel('ttype', "link_tournamenttype");
	}

	/**
	 * Módosít.
	 *
	 */
	private function action_modTournamentType() {
		AdminPageHelper::action_mod($this, 'modTournamentType', 'ttype', 'tournamenttype', "compt_mod_success", "compt_mod_err", "link_tournamenttype");
	}

	/**
	 * Töröl.
	 *
	 */
	private function action_delTournamentType() {
		AdminPageHelper::action_del('delTournamentType', 'tournamenttype', "compt_del_success", "compt_del_err", "link_tournamenttype");
	}


	/**
	 * Hozzáad.
	 */
	private function action_addTournamentType() {
		AdminPageHelper::action_add($this, 'addTournamentType', 'newttype', 'tournamenttype', "compt_add_success", "compt_add_err", "link_tournamenttype");
	}

	public function checkData($type, $values) {
		if (($type == 'mod' && empty($values['name'])) || ($type == 'add' && empty($values['newname']))) {
			throw new ParameterException(TextHandler::getText("compt_err_emptyname"));
		}
	}

}
?>